package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.GatewayIpLimit;
import com.tebiecloud.base.server.service.GatewayIpLimitService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 开放网关-IP访问控制-策略 前端控制器
 * @author tebie
 * @date 2019-08-08
 */
@RestController
@RequestMapping("gatewayiplimit")
public class GatewayIpLimitController {

    @Autowired
    private GatewayIpLimitService gatewayIpLimitService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "/list")
    public ResultBody<IPage<GatewayIpLimit>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        GatewayIpLimit query = pageParams.mapToObject(GatewayIpLimit.class);
        QueryWrapper<GatewayIpLimit> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(gatewayIpLimitService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ResultBody<GatewayIpLimit> get(@RequestParam("id") Long id) {
        GatewayIpLimit entity = gatewayIpLimitService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param policyName 策略名称,
     * @param policyType 策略类型:0-拒绝/黑名单 1-允许/白名单,
     * @param ipAddress ip地址/IP段:多个用隔开;最多10个
     * @return
     */
    @PostMapping("/add")
    public ResultBody add(
        @RequestParam(value = "policyName", required = false) String policyName,
        @RequestParam(value = "policyType", required = false) Integer policyType,
        @RequestParam(value = "ipAddress", required = false) String ipAddress
    ) {
        GatewayIpLimit entity = new GatewayIpLimit();
        entity.setPolicyName(policyName);
        entity.setPolicyType(policyType);
        entity.setIpAddress(ipAddress);
        gatewayIpLimitService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param policyName 策略名称
     * @param policyType 策略类型:0-拒绝/黑名单 1-允许/白名单
     * @param ipAddress ip地址/IP段:多个用隔开;最多10个
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(
            @RequestParam(value = "policyId", required = false) Long policyId,
            @RequestParam(value = "policyName", required = false) String policyName,
            @RequestParam(value = "policyType", required = false) Integer policyType,
            @RequestParam(value = "ipAddress", required = false) String ipAddress
    ) {
        GatewayIpLimit entity = new GatewayIpLimit();
        entity.setPolicyId(policyId);
        entity.setPolicyName(policyName);
        entity.setPolicyType(policyType);
        entity.setIpAddress(ipAddress);
        gatewayIpLimitService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("/remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            gatewayIpLimitService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            gatewayIpLimitService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
